package com.hemh.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.hemh.entity.OmsBook;
import com.hemh.service.BookService;
import com.hemh.utils.ResponseUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Controller
@Api(value = "Book相关操作的接口")
public class BookController {

	private final Log logger = LogFactory.getLog(BookController.class);

	@Resource
	private BookService bookService;

	@PostMapping("/book/insert")
	@ApiOperation(value = "图书新增接口")
	@ResponseBody
	public Object addBook(@RequestBody JSONObject jsonParam) {
		int result = 0;
		logger.info("--->>>接收的参数 = " + jsonParam);
		result = bookService.insertBook(jsonParam);
		logger.info("--->>>图书新增结果, result = " + result);
		return ResponseUtil.ok(result);
	}

	@PostMapping("/book/bookList")
	@ApiOperation(value = "图书列表查询接口")
	@ResponseBody
	public Object getBookList(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize) {
		List<OmsBook> bookList = new ArrayList<OmsBook>();
		/** 查询图书列表, 不查询已经销毁的, 分页查询 */
		bookList = bookService.getBookInfoNoDeleted(page, pageSize);
		return ResponseUtil.ok(bookList);
	}

	@PostMapping("/book/bookInfoByName")
	@ApiOperation(value = "根据图书名称查询图书信息接口")
	@ResponseBody
	public Object getBookInfoByName(@RequestParam String bookName, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize) {
		List<OmsBook> bookList = new ArrayList<OmsBook>();
		bookList = bookService.getBookInfoByName(bookName, page, pageSize);
		return ResponseUtil.ok(bookList);
	}

	@PostMapping("/book/updateBookStoreById")
	@ApiOperation(value = "图书库存状态接口")
	@ResponseBody
	public Object updateBookStoreById(@RequestParam Integer bookId, @RequestParam String isStore) {
		int result = 0;
		result = bookService.updateBookStoreById(bookId, isStore);
		return ResponseUtil.ok(result);
	}

	@PostMapping("/book/updateBookDestroyById")
	@ApiOperation(value = "图书删除接口")
	@ResponseBody
	public Object updateBookDestroyById(@RequestParam Integer bookId, @RequestParam String isDeleted) {
		int result = 0;
		result = bookService.updateBookDestroyById(bookId, isDeleted);
		return ResponseUtil.ok(result);
	}

	@PostMapping("/book/update")
	@ApiOperation(value = "图书信息更新接口")
	@ResponseBody
	public Object updateBookInfo(@RequestBody JSONObject jsonParam) {
		int result = 0;
		logger.info("--->>>接收的参数 = " + jsonParam);
		/** 图书信息更新逻辑，通过图书主键id更新 */
		result = bookService.updateBookInfoById(jsonParam);
		return ResponseUtil.ok(result);
	}
	
	@PostMapping("/book/exportBookTemplate")
	@ApiOperation(value = "图书模板导出excel接口")
	@ResponseBody
	public void exportBookTemplate(HttpServletRequest request, HttpServletResponse response) {
		bookService.exportBookTemplate(request, response);		
	}
	
	@PostMapping("/book/exportBookInfo")
	@ApiOperation(value = "图书列表导出excel接口")
	@ResponseBody
	public void exportBookInfo(HttpServletRequest request, HttpServletResponse response) {
		bookService.exportBookInfo(request, response);
	}

	@PostMapping(value="/book/importBookInfo", consumes = "multipart/*", headers = "content-type=multipart/form-data")
	@ApiOperation(value = "图书信息excel导入接口")
	@ResponseBody
	public Object importBookInfo(@ApiParam(value = "用户信息Excel导入数据", required = true) MultipartFile file) {
		int result = 0;
		/** 图书信息导入逻辑, 导入所有书籍信息的字段默认store=1, delete=0 */
		bookService.importBookInfo(file);		
		return ResponseUtil.ok(result);
	}

	@PostMapping("/book/borrow")
	@ApiOperation(value = "图书借阅接口")
	@ResponseBody
	public Object borrowBook(@RequestBody JSONObject jsonParam) {
		int result = 0;
		logger.info("--->>>接收的参数 = " + jsonParam);
		result = bookService.bookBrrow(jsonParam);		
		return ResponseUtil.ok(result);
	}
	
	@PostMapping("/book/returnBook")
	@ApiOperation(value = "图书归还接口")
	@ResponseBody
	public Object returnBook(@RequestParam String userNumber, @RequestParam Integer bookId) {
		int result = 0;
		result = bookService.returnBook(userNumber, bookId);		
		return ResponseUtil.ok(result);
	}
	
	@PostMapping("/book/updateBookBorrowDeleted")
	@ApiOperation(value = "更新图书借还记录删除状态接口")
	@ResponseBody
	public Object updateBookBorrowDeleted(@RequestParam Integer bookBorrowId, @RequestParam String deleted) {
		int result = 0;
		result = bookService.updateBookBorrowDeletedById(bookBorrowId, deleted);	
		return ResponseUtil.ok(result);
	}
}
